package cn.edu.lingnan.tourism.service.Impl;

import cn.edu.lingnan.tourism.exeption.TourismException;
import cn.edu.lingnan.tourism.mapper.AdminMapper;
import cn.edu.lingnan.tourism.mapper.RouteMapper;
import cn.edu.lingnan.tourism.mapper.SellerMapper;
import cn.edu.lingnan.tourism.mapper.UserMapper;
import cn.edu.lingnan.tourism.pojo.Route;
import cn.edu.lingnan.tourism.pojo.Seller;
import cn.edu.lingnan.tourism.pojo.User;
import cn.edu.lingnan.tourism.service.AdminService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private RouteMapper routeMapper;
    @Resource
    private SellerMapper sellerMapper;
    @Resource
    private AdminMapper adminMapper;
    @Transactional(readOnly = true)
    @Override
    public List<User> getAllUsers() throws TourismException {
        List<User> users= userMapper.selectAll();
        if(users!=null){
            return users;
        }else {
            throw new TourismException("查询失败");
        }
    }
    @Transactional(readOnly = true)
    @Override
    public List<Route> getAllRoutes() throws TourismException {
        List<Route> Routes= routeMapper.selectAll();
        if(Routes!=null){
            return Routes;
        }else {
            throw new TourismException("查询失败");
        }
    }
    @Transactional(readOnly = true)
    @Override
    public List<Seller> getAllSellers() throws TourismException {
        List<Seller> Sellers= sellerMapper.selectAll();
        if(Sellers!=null){
            return Sellers;
        }else {
            throw new TourismException("查询失败");
        }
    }
    @Transactional(rollbackFor = TourismException.class)
    @Override
    public int deleteUser(Integer uid) throws TourismException {
try{
    adminMapper.deleteFavoriteByUid(uid);
    int flag = userMapper.deleteByPrimaryKey(uid);
    if(flag==1){
        return flag;
    }else {
        throw new TourismException("用户已被删除");
    }
}catch (Exception e){
    throw new TourismException("删除失败");
}
    }
    @Transactional(rollbackFor = TourismException.class)
    @Override
    public int deleteRoute(Integer rid) throws TourismException {
        try{
            int flag;
            adminMapper.deleteRouteImgByRid(rid);
            adminMapper.deleteFavoriteByRid(rid);
            flag = routeMapper.deleteByPrimaryKey(rid);
            if(flag==1){
                return flag;
            }else {
                throw new TourismException("路线已被删除");
            }
        }catch (Exception e){
            throw new TourismException("删除失败");
        }

    }
    @Transactional(rollbackFor = TourismException.class)
    @Override
    public int deleteSeller(Integer sid) throws TourismException {
        try{
            int flag;
            adminMapper.deleteRouteBySid(sid);
            flag = sellerMapper.deleteByPrimaryKey(sid);
            if(flag==1){
                return flag;
            }else {
                throw new TourismException("商家已被删除");
            }
        }catch (Exception e){
            throw new TourismException("删除失败");
        }

    }
    @Transactional(rollbackFor = TourismException.class)
    @Override
    public int updateUser(User user) throws TourismException {
        try{
            int flag = userMapper.updateByPrimaryKeySelective(user);
            if(flag==1){
                return flag;
            }else {
                throw new TourismException("用户已被删除");
            }
        }catch (Exception e){
            throw new TourismException("更新失败");
        }

    }
    @Transactional(rollbackFor = TourismException.class)
    @Override
    public int updateRoute(Route route) throws TourismException {
        try{
            int flag = routeMapper.updateByPrimaryKeySelective(route);
            if(flag==1){
                return flag;
            }else {
                throw new TourismException("路线已被删除");
            }
        }catch (Exception e){
            throw new TourismException("更新失败");
        }

    }
    @Transactional(rollbackFor = TourismException.class)
    @Override
    public int updateSeller(Seller seller) throws TourismException {
        try{
            int flag = sellerMapper.updateByPrimaryKeySelective(seller);
            if(flag==1){
                return flag;
            }else {
                throw new TourismException("商家已被删除");
            }
        }catch (Exception e){
            throw new TourismException("更新失败");
        }

    }
    @Transactional(rollbackFor = TourismException.class)
    @Override
    public int addUser(User user) throws TourismException {
        try{
            int flag = userMapper.insertSelective(user);
            if(flag==1){
                return flag;
            }else {
                throw new TourismException("用户已存在");
            }
        }catch (Exception e){
            throw  new TourismException("添加失败");
        }

    }
    @Transactional(rollbackFor = TourismException.class)
    @Override
    public int addRoute(Route route) throws TourismException {
        try{
            int flag = routeMapper.insertSelective(route);
            if(flag==1){
                return flag;
            }else {
                throw new TourismException("路线已存在");
            }
        }catch (Exception e){
            throw new TourismException("添加失败");
        }

    }
    @Transactional(rollbackFor = TourismException.class)
    @Override
    public int addSeller(Seller seller) throws TourismException {
        try{
            int flag = sellerMapper.insertSelective(seller);
            if(flag==1){
                return flag;
            }else {
                throw new TourismException("添加失败");
            }
        }catch (Exception e){
            throw new TourismException("商家已存在");
        }

    }
    @Transactional(readOnly = true)
    @Override
    public List<User> selectUserByString(String str) {
        List<User> users = userMapper.selectUserByString(str);
        return users;
    }
    @Transactional(readOnly = true)
    @Override
    public List<Seller> selectSellerByString(String str) {
        return sellerMapper.selectSellerByString(str);
    }
    @Transactional(readOnly = true)
    @Override
    public List<Route> selectRouteByString(String str) {
        return routeMapper.selectRouteByString(str);
    }
    @Transactional(readOnly = true)
    @Override
    public Map<String, Integer> getAllNumber() {
Map<String,Integer> map=new HashMap<>();
        Integer routeCount = routeMapper.selectRouteCount();
        map.put("route",routeCount);
        Integer sellerCount = sellerMapper.selectSellerCount();
        map.put("seller",sellerCount);
        Integer useCount = userMapper.selectUseCount();
        map.put("user",useCount);
        return map;
    }
}
